package com.jrh.biz.user.like;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.jrh.biz.product.ProductService;
import com.jrh.dal.UserLikeDao;
import com.jrh.domain.entity.Product;
import com.jrh.domain.entity.UserLike;
import com.jrh.domain.like.DoLikeResult;

/**
 * 用户喜欢写服务
 * 
 * @author jasen.hsu.cn@gmail.com 2012-8-8 下午2:37:12
 */
public class UserLikeWriteServiceImpl implements UserLikeWriteService {

    private static final Logger log = LoggerFactory.getLogger(UserLikeWriteServiceImpl.class);

    private UserLikeDao         userLikeDao;

    private ProductService      productService;

    private TransactionTemplate transactionTemplate;

    public DoLikeResult like(final int type, final Long userId, final Long id) {
        final DoLikeResult result = new DoLikeResult();
        /** 未登录 */
        if (userId == null) {
            result.setError(DoLikeResult.ERROR_CODE_NO_lOGIN);
            return result;
        }
        /** 参数错误 */
        if (type == 0 || id == null) {
            result.setError(DoLikeResult.ERROR_CODE_ILLEGAL_ARGUMENT);
            return result;
        }

        Integer count = userLikeDao.countByRef(userId, id, type);
        if (count != null && count != 0) {// 已存在
            result.setError(DoLikeResult.ERROR_CODE_LIKED);
            return result;
        }
        boolean success = (Boolean) transactionTemplate.execute(new TransactionCallback<Boolean>() {

            @Override
            public Boolean doInTransaction(TransactionStatus status) {
                try {
                    /** 创建喜欢记录 */
                    userLikeDao.create(userId, id, type);
                    switch (type) {
                        case 0:
                            result.setError(DoLikeResult.ERROR_CODE_ILLEGAL_ARGUMENT);
                            break;
                        case UserLike.LIKE_REF_TYPE_PRODUCT:
                            /** 增加产品喜欢人数 */
                            userLikeDao.addProductLike(id);
                            Product p = productService.getProductById(id);
                            if (p != null && p.getBrandId() != null) {
                                Integer userLikeBrandCount = userLikeDao.countByRef(userId, p.getBrandId(),
                                                                                    UserLike.LIKE_REF_TYPE_BRAND);
                                // 未喜欢过该品牌
                                if (userLikeBrandCount == null || userLikeBrandCount == 0) {
                                    userLikeDao.addBrandLike(p.getBrandId());
                                    try {
                                        userLikeDao.create(userId, p.getBrandId(), UserLike.LIKE_REF_TYPE_BRAND);
                                    } catch (Exception e) {
                                        log.error("like brand error:", e);
                                    }
                                }
                            }
                            break;
                        case UserLike.LIKE_REF_TYPE_ATLAS:
                            /** 增加产品喜欢人数 */
                            userLikeDao.addAtlasLike(id);
                            break;
                        case UserLike.LIKE_REF_TYPE_SHOWROOM:
                            /** 增加产品喜欢人数 */
                            userLikeDao.addShowroomLike(id);
                            break;
                        default:
                            result.setError(DoLikeResult.ERROR_CODE_ILLEGAL_ARGUMENT);
                            break;
                    }
                    return Boolean.TRUE;
                } catch (Exception e) {
                    log.error("save like error,type:+" + type + ",  userId:" + userId + ",  id:" + id + "   ", e);
                    result.setError(DoLikeResult.ERROR_CODE_SYS);
                    status.setRollbackOnly();
                }
                return Boolean.FALSE;
            }
        });
        if (success) {
            result.setError(DoLikeResult.SUCCESS);
        }
        result.setSuccess(success);
        return result;
    }

    /**
     * 去除喜欢
     * 
     * @param type
     * @param id
     * @return
     */
    public DoLikeResult unlike(int type, Long userId, Long id) {
        DoLikeResult result = new DoLikeResult(true, DoLikeResult.SUCCESS);
        userLikeDao.delete(userId, id, type);
        return result;
    }

    public void setUserLikeDao(UserLikeDao userLikeDao) {
        this.userLikeDao = userLikeDao;
    }

    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

}
